
(function () {
  if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    return
  }
    
  $(function () {
    var $chepterNav = $('.chepter-nav')
    $chepterNav.find('h2').eq(0).addClass('active')
    $chepterNav.find('a').on('click', function(ev) {
      ev.preventDefault()
      var $this = $(this)
      var href = $(this).attr('href')
      $('html, body').stop(false).animate({
        scrollTop: $(href).offset().top}, 
        500, 
        function() {
          $chepterNav.find('h2, h3').removeClass('active')
          $this.parent().addClass('active')
        })
    })



    var $pre = $('pre')
    $pre.addClass('brush: js line-numbers  language-js').find('code').removeClass().addClass('language-js')
    $pre.each(function () {
      var $this = $(this)
      var $tools = $(`
          <div class="codetool" style="display:none;">
            <div class="codetool-inner">
            <div class="select-code el-tooltip tool-icon" data-toggle="tooltip" data-placement="top" title="全选"></div>
            <div class="copy-code tool-icon" data-toggle="tooltip" data-placement="top" data-clipboard-text="" title="复制"></div>
            </div>
          </div>`)

      $this.before($tools)
      var $copy = $tools.find(".copy-code")

      $('[data-toggle="tooltip"]').tooltip({
        container: "body"
      })
      
      var timer = null;
      $this.on({
        mouseover: function() {
          clearTimeout(timer)
          $tools.show()
        },
        mouseout: function() {
          timer = setTimeout(function () {
            $tools.hide()
          }, 200)
        }
      });
      $tools.on({
        mouseover: function() {
          clearTimeout(timer)
        },
        mouseout: function() {
          timer = setTimeout(function () {
            $tools.hide()
          }, 200)
        }
      });

      $copy.attr('data-clipboard-text', $this.text())

      $tools.find(".select-code").click(function(e) {
        selectEl($this.get(0))
      })
    })
    
    var copyEl = new ClipboardJS('.copy-code')
    copyEl.on("success", function(e) {
      $('.copy-code').attr('data-original-title', '复制')
      $(e.trigger).attr('data-original-title', '已复制').tooltip('show')
      e.clearSelection()
    })
    copyEl.on("error", function(e) {
      $(e.trigger).prev().trigger("click"),
      $('.copy-code').attr("data-original-title", "复制"),
      $(e.trigger).attr("data-original-title", "按 ⌘+c 复制").tooltip("show")
    })

    function selectEl(el) {
      var range = document.createRange();
      range.selectNodeContents(el);

      var select = window.getSelection();
      select.removeAllRanges();
      select.addRange(range);
    }

    $(window).on('scroll', function () {
      var currentScroll = $(this).scrollTop()
      
      $('.doc-body').find('h2, h3').each(function () {
        var hPositon = $(this).offset().top
        if (hPositon < currentScroll + 60 + 20) {
          var id = $(this).attr('id');
          $chepterNav.find('a').closest('h2, h3').removeClass('active');
          $chepterNav.find('[href=#'+id+']').closest('h2, h3').addClass('active');
        }
      })
    })
  })
})()
